clear
fid=fopen('RLS_input_0528.txt','r');
filename=fscanf(fid,'%80s /n');
Hz=fscanf(fid,'%12f /n')
DOF=fscanf(fid,'%12f /n');
N_B=fscanf(fid,'%12f /n');
N_L=fscanf(fid,'%12f /n');
IJ_B=fscanf(fid,'%12f /n');
dIJ=fscanf(fid,'%12f /n');
IJ_E=fscanf(fid,'%12f /n');
FF=fscanf(fid,'%12f /n');
Bt=fscanf(fid,'%12f /n');
Bn=fscanf(fid,'%12f /n');
out_filename=fscanf(fid,'%60s /n');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
out_form=[];
for i_1=1:DOF+3
    out_form=[out_form '%10.7f'];
end
out_form=[out_form ' \n '];
X0=load(filename);
[XR XC]=size(X0);  
X3=X0;
dt=1/Hz;
NN0=N_B*Hz;
NN=N_L*Hz;
output=[1:DOF];input=[DOF+1:XC];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%[PHI,PSI,XVAL]=wavefun(wname,10);
%for i_1=1:NS
%    XVAL_0(i_1,:)=XVAL*S_0(i_1);
%    XVAL_00=[min(XVAL_0(i_1,:)):1/Hz:max(XVAL_0(i_1,:))];
%    PSI_00=spline(XVAL_0(i_1,:),PSI,XVAL_00);
%    Con_00=zeros(XC*(Bn+1),length(PSI_00));
%    PSI_0{i_1}=PSI_00;
%    Con_0{i_1}=Con_00;
%end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X1=[];
for i_1=NN+1:XR
    T=((i_1-N_B-1)/Hz);
    if Bt==1
        T=T/(XR/Hz);type=['poly'];
        S_W=[];
        for i_2=0:Bn
            S_W=[S_W T^(i_2)];
        end
    elseif Bt==2
        T=T/(XR/Hz);type=['sin'];
        S_W=1;
        for i_2=1:Bn
            S_W=[S_W sin(T*(i_2)/30)];
        end
    end
 

    for i_2=IJ_B:dIJ:IJ_E
        IJ=i_2;
    if i_1==NN+1;
        REX_00{i_2}=[];
        X4=[];
        X5=[];
        for i_3=1:XC%NS
            X4=[X4; X3(NN0+1:NN,1:DOF)];
            X5_out=[];X5_in1=[];X5_in2=[];
            for i_4=1:IJ
                X5_out=[X5_out X3(NN0+1-i_4:NN-i_4,1:DOF)];        
                X5_in2=[X5_in2 X3(NN0+1-i_4:NN-i_4,DOF+1:XC)];
            end
            X5_in1=X3(NN0+1:NN,DOF+1:XC);
            X5=[X5;X5_out X5_in1 X5_in2];
        end
        P_0{i_2}=inv(X5'*X5);
        r_0{i_2}=(X5'*X5)\(X5'*X4);
%        RASSIS=[out_filename 'K_' num2str(Bn) '_FF_' num2str(FF) '_' type '_IJ_' num2str(i_7) '_' num2str(wname) '_d.dat'];
%        fid01=fopen(RASSIS,'w');
    else
            X6=[];X7=[];
            for i_11=1:1%NS
                X6=[X6;X3(i_1,1:DOF)];
                X7_out=[];X7_in1=[];X7_in2=[];
            for i_12=1:IJ
                X7_out=[X7_out X3(i_1-i_12,1:DOF)];
                X7_in2=[X7_in2 X3(i_1-i_12,DOF+1:XC)];
            end
            X7_in1=X3(i_1,DOF+1:XC);
            X7=[X7;X7_out X7_in1 X7_in2];
            end
            L=(P_0{i_2}*X7')/(FF*eye(1)+X7*P_0{i_2}*X7');%*1e10
            E=X6-X7*r_0{i_2};
            r=r_0{i_2}+L*E;
            I=eye((DOF+1)*IJ+1);
            H=kron(I,1);%S_W
            theta=(H'*r)';
            G_2=zeros((DOF*(IJ-1)),DOF);
            G_3=eye((DOF*(IJ-1)),(DOF*(IJ-1)));
            G_4=[G_2 G_3];
            G_1=[];
            for i_13=1:IJ
                G_1=[theta(:,(DOF*(i_13-1)+1):DOF*i_13) G_1];
            end
            G_5=[G_4;G_1];
            [beta_k,Xi,V_4,REX]=E_V_03(G_5,DOF,IJ,Hz,1);
            REX(1,:)=(i_1)/Hz;
            REX_00{i_2}=[REX_00{i_2};REX'];
            P=(P_0{i_2}-L*X7*P_0{i_2})/FF;
            P_0{i_2}=P;
            r_0{i_2}=r;
%            fprintf(fid01,out_form,REX);
    end
    end
    
end

for i_1=IJ_B:dIJ:IJ_E
    IJ=i_1;
    RASSIS=[out_filename 'K_' num2str(Bn) '_FF_' num2str(FF) '_' type '_IJ_' num2str(i_1) '_v.dat'];
    fin_res=REX_00{i_1}
    save(RASSIS,'fin_res','-ascii')
end

RASSIS